package com.cskaoyan.mall.search.Controller;

import com.cskaoyan.mall.commons.constant.SysRetCodeConstants;
import com.cskaoyan.mall.commons.result.ResponseData;
import com.cskaoyan.mall.commons.result.ResponseUtil;
import com.cskaoyan.mall.dto.AllItemResponse;
import com.cskaoyan.mall.dto.ItemDto;
import com.cskaoyan.mall.search.api.ProductApi;
import com.cskaoyan.mall.search.consts.SearchRetCode;
import com.cskaoyan.mall.search.dto.SearchRequest;
import com.cskaoyan.mall.search.dto.SearchResponse;
import com.cskaoyan.mall.search.form.SearchPageInfo;
import com.cskaoyan.mall.search.service.InitDataService;
import com.cskaoyan.mall.search.service.ProductSearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: 牟治宇
 * @description 搜索接口
 * @date: 2022/8/8 9:31
 */
@RestController
@RequestMapping("shopping/search")
public class ProductSearchController {
    @Autowired
    private InitDataService initDataService;

    @Autowired
    private ProductSearchService productSearchService;


    @GetMapping("init")
    public ResponseData initProductDate() {
        initDataService.initItems();
        return new ResponseUtil<>().setData(null, "success");
    }

    @GetMapping("{key}")
    public ResponseData getSearchTips(@PathVariable("key") String keyword) {
        SearchRequest searchRequest = new SearchRequest();
        searchRequest.setKeyword(keyword);

        SearchResponse searchResponse = productSearchService.fuzzySearch(searchRequest);

        if (searchResponse.getCode().equals(SearchRetCode.SUCCESS.getCode())) {
            return new ResponseUtil<>().setData(searchResponse.getData());
        }

        return new ResponseUtil<>().setErrorMsg(searchResponse.getMsg());
    }

    @PostMapping
    public ResponseData search(@RequestBody SearchPageInfo searchPageInfo) {

        // if (searchPageInfo.getKey() == null) {
        //     return new ResponseUtil().setErrorMsg(Integer.valueOf(SysRetCodeConstants.REQUEST_FORMAT_ILLEGAL.getCode()),SysRetCodeConstants.REQUEST_FORMAT_ILLEGAL.getMessage());
        // }

        SearchRequest request = new SearchRequest();

        request.setCurrentPage(searchPageInfo.getPage());
        request.setPageSize(searchPageInfo.getSize());
        request.setPriceGt(searchPageInfo.getPriceGt());
        request.setPriceLte(searchPageInfo.getPriceLte());
        request.setSort(searchPageInfo.getSort());
        request.setKeyword(searchPageInfo.getKey());


        SearchResponse response = productSearchService.search(request);

        if (SearchRetCode.SUCCESS.getCode().equals(response.getCode())) {
            Map<String, Object> responseMap = new HashMap<>();
            responseMap.put("total", response.getTotal());
            responseMap.put("data", response.getData());
            return new ResponseUtil().setData(responseMap);
        }
        return new ResponseUtil().setErrorMsg(response.getMsg());


    }
}
